package com.zsjt.app.compoent.utils.common;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class BASE64 {
    /**
     * base64加密
     * @param cleartext
     * @return
     */
    public static String encodeBase64(String cleartext) {
        try{
            cleartext = Base64.getEncoder().encodeToString(cleartext.getBytes("UTF-8"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return cleartext;
    }

    /**
     * base64解密
     * @param ciphertext
     * @return
     */
    public static String decodeBase64(String ciphertext){
        try {
            ciphertext = new String(Base64.getDecoder().decode(ciphertext), StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ciphertext;
    }

    public static void main(String[] args)  {
        String str = "asdasdasdasdasd";
        //编码加密
        String encodeStr = encodeBase64(str);
        System.out.println("加密后的字符串为:" + encodeStr);
        //解码解密
        String decoderStr = decodeBase64(encodeStr);
        // 推荐使用StandardCharsets类指定
        System.out.println("解密后的字符串为" + decoderStr);
    }
}
